Home:ALL Converter>Accessing a PHP stdClass Object in Javascript/Smarty

Accessing a PHP stdClass Object in Javascript/Smarty

Ask Time:2012-06-22T04:18:58         Author:CodeMonkey

Json Formatter

I have a stclass object which i am initializing in PHP code. The object returns

object(stdClass)[238]
  public '0' => string 'Jun 20, 2012 03:02 PM' (length=21)
  public '1' => string 'Jun 20, 2012 03:26 PM' (length=21)
  public '2' => string 'Jun 21, 2012 01:12 PM' (length=21)

on doing var_dum($myObjectName)

I am passing this to smarty template where i need to access values of stdClass object at index [i] based on a javascript variable .

I have tried several of these:

{/literal} {$myObjectName}{literal} 
{/literal} {$myObjectName.0}{literal} 
{/literal} {$myObjectName.'0'}{literal} 
{/literal} {$myObjectName.'myLocalJSVariable'}{literal} 

but i am not bale to fetch value of Object at particular index

Author:CodeMonkey,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11146085/accessing-a-php-stdclass-object-in-javascript-smarty
rodneyrehm :

May I suggest you use json_encode() to dump your data to json, save that to something accessible to javascript, and then access your data from javascript?\n\n{$myObjectName->javaScriptVariable} sounds just like you were trying to access some client data on the server, or some server data on the client - which is not possible unless you provide the data.",
2012-06-22T14:31:56
lorenzo-s :

Did you tried {$myObjectName->0}?\n\nIf you want to use it the way you use arrays, you must pass an array version of the object to Smarty:\n\n$smarty->assign('myObjectName', get_object_vars($obj));\n\n\nThe get_object_vars() method returns a named array of all properties from an object.",
2012-06-21T20:21:48
yy